package Servlet;

import AccesoDatos.*;
import Entidades.*;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import com.google.gson.*;



public class SVerificarUsuario extends HttpServlet {

    protected void processRequest(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException, Exception {
        //response.setContentType("text/html;charset=UTF-8");
        //PrintWriter out = response.getWriter();
        String user = request.getParameter("txtUsuario"); 
        String clave = request.getParameter("txtClave");

        Usuario uc = datUsuario.VerificaUsuarioClienteObjeto(user, clave);
        Usuario ue = datUsuario.VerificaUsuarioEmpleadoObjeto(user, clave);

        if (uc != null) {
            HttpSession sesion = request.getSession(true);
            sesion.setAttribute("usuario", uc);
            response.sendRedirect("/Cliente/Index.jsp");
        } else {

            if (ue != null) {
                HttpSession sesion = request.getSession(true);
                sesion.setAttribute("usuario", ue);
                response.sendRedirect("/Administrador/Index.jsp");
            } else {
                response.sendRedirect("/Index.jsp");
            }
        }
    }

    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        try {
            processRequest(request, response);
        } catch (Exception ex) {
            Logger.getLogger(SVerificarUsuario.class.getName()).log(Level.SEVERE, null, ex);
        }
    }

    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        try {
            processRequest(request, response);
            
            PrintWriter out = response.getWriter();
            
        //response.setContentType("text/html");
     
        response.setContentType("application/json");
        response.setCharacterEncoding("UTF-8");
        
        String Username = request.getParameter("usuario").trim();
        String Password = request.getParameter("contraseña").trim();
            
        
        Usuario uc = datUsuario.VerificaUsuarioClienteObjeto(Username, Password);
        
        //si el usuario no existe
         if (uc == null) {
           
            JsonObject myObj = new JsonObject();
            myObj.addProperty("aviso", false);
            out.println(myObj.toString());
            
        }
         //si el usuario existe
         else {
                Gson gson = new Gson();
                JsonElement Usuario = gson.toJsonTree(uc);
                JsonObject myObj = new JsonObject();
                myObj.addProperty("aviso", true);
                myObj.add("usuario", Usuario);
                out.println(myObj.toString());
              }
        
        out.close();
        
        } catch (Exception ex) {
            Logger.getLogger(SVerificarUsuario.class.getName()).log(Level.SEVERE, null, ex);
        }
    }

    @Override
    public String getServletInfo() {
        return "Short description";
    }
}
